package com.tentative.competition.common.core.result;

import java.util.List;

public class ApiResult<T> {

	public final static int CODE_OK = 200;// 成功
	public final static int CODE_NOT_PASS = 401;// uri权限校验不通过
	public final static int CODE_NOT_ALLOW = 3425;// 不允许
	public final static int CODE_PARAM_ERR = 3406;// 参数错误
	public final static int CODE_SERVER_ERR = 402;// 服务器异常

	private int code = CODE_OK;
	private String msg = "OK";
	private T data = null;
	private boolean success = true;

	public ApiResult(){

	}

	public ApiResult(T data) {
		this.data = data;
	}

	public ApiResult(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public ApiResult(int code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public static <T> ApiResult<T> ok() {
		return new ApiResult<>();
	}

	public static <T> ApiResult<T> ok(T data) {
		return new ApiResult<>(data);
	}

	public static <T> ApiResult<T> notPass() {
		return new ApiResult<>(ApiResult.CODE_NOT_PASS, "权限不足，无法操作该资源", null);
	}

	public static <T> ApiResult<T> notPass(String msg) {
		return new ApiResult<>(ApiResult.CODE_NOT_PASS, msg, null);
	}

	public static <T> ApiResult<T> notAllow() {
		return new ApiResult<>(ApiResult.CODE_NOT_ALLOW, "未经许可的用户", null);
	}

	public static <T> ApiResult<T> notAllow(String msg) {
		return new ApiResult<>(ApiResult.CODE_NOT_ALLOW, msg, null);
	}

	public static <T> ApiResult<T> paramError() {
		return new ApiResult<>(ApiResult.CODE_PARAM_ERR, "参数解析错误", null);
	}

	public static <T> ApiResult<T> paramError(String msg) {
		return new ApiResult<>(ApiResult.CODE_PARAM_ERR, msg, null);
	}

	public static <T> ApiResult<T> serverError() {
		return new ApiResult<>(ApiResult.CODE_SERVER_ERR, "服务器异常", null);
	}

	public static <T> ApiResult<T> serverError(String msg) {
		return new ApiResult<>(ApiResult.CODE_SERVER_ERR, msg, null);
	}

	public static <T> ApiResult<T> newResult(int code, String msg, T data) {
		return new ApiResult<>(code, msg, data);
	}

	public static <T> ApiResult<T> newResult(int code, String msg) {
		return new ApiResult<>(code, msg);
	}

	public static <T> ApiResult<PageResult<T>> ofPage(List<T> content, long totalCount){
		PageResult<T> pageResult = new PageResult<>();
		pageResult.setContent(content);
		pageResult.setTotal(totalCount);
		return ok(pageResult);
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public boolean isSuccess() {
		return this.code == CODE_OK;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

}
